/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.mdb;

import com.bionic.ce.entities.User;
import com.bionic.ce.entitybeans.user.UserFacadeLocal;
import java.util.List;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

/**
 *
 * @author Artem
 */
@MessageDriven(mappedName = "jms/CurrencyExchangeTopic", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MessageBean implements MessageListener {

    @EJB
    private UserFacadeLocal userEJB;

    public MessageBean() {
    }

    public UserFacadeLocal getUserEJB() {
        return userEJB;
    }

    public void setUserEJB(UserFacadeLocal userEJB) {
        this.userEJB = userEJB;
    }


    @Override
    public void onMessage(Message message) {
        ObjectMessage msg = null;
        try {
            if (message instanceof ObjectMessage) {
                msg = (ObjectMessage) message;
                User user = (User) msg.getObject();
                System.out.println("User login =" + user.getEmail());
                System.out.println("User password =" + user.getPassword());
                List<User> userList=userEJB.findByDynamicSelect(user);
                boolean b = userList.isEmpty();
                if (!b) {
                    user=(User)userList.toArray()[0];
                    System.out.println("User Approved");              
                }
            }
        } catch (JMSException c) {
            c.printStackTrace();
        }
    }
}
